package com.client.controller;


import com.client.common.R;
import com.client.dto.GoodsAndLanguageDto;
import com.client.pojo.FlowerShopping;
import com.client.service.FlowerShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 晚云
 * @since 2023-09-12
 */
@CrossOrigin
@RestController
@RequestMapping("/flower-shopping")
public class FlowerShoppingController {

    @Autowired
    private FlowerShoppingService flowerShoppingService;

    /**
     * 查询用户的购物车
     * @param userId
     * @return
     */
    @GetMapping("/init/{userId}")
    public R init(@PathVariable("userId") String userId){
        if (userId.equals("")){
            return R.error("请登录");
        }
        List<FlowerShopping> flowerShoppings = flowerShoppingService.selectByUserId(userId);
        return R.success(flowerShoppings);
    }

    /**
     * 删除购物车里面的商品
     * @param goodsId
     * @return
     */
    @DeleteMapping("/delete/{goodsId}/{userId}")
    public R delete(@PathVariable("goodsId") String goodsId, @PathVariable("userId") String userId){
        boolean flag = flowerShoppingService.deleteByGoodsId(goodsId,userId);
        if (flag){
            return R.success("删除成功！");
        }else {
            return R.error("删除失败！");
        }
    }

    /**
     * 创建订单，删除购物车数据
     * @param shopping
     * @return
     */
    @PostMapping("/insert")
    public R insert(@RequestBody List<FlowerShopping> shopping){
        boolean flag = flowerShoppingService.insertListAndDeleteShopping(shopping);
        if (flag){
            return R.success("付款成功！！！");
        }else {
            return R.error("出现了错误！请重新");
        }
    }

    @PostMapping("/join")
    public R join(@RequestBody GoodsAndLanguageDto goodsAndLanguageDto){
        boolean flag = flowerShoppingService.joinShopping(goodsAndLanguageDto);
        if (flag){
            return R.success("添加到购物车成功！");
        }else {
            return R.error("添加购物车失败，请重试");
        }
    }

    @PostMapping("/pay")
    public R pay(@RequestBody GoodsAndLanguageDto dto) {
        boolean flag = flowerShoppingService.insertOrder(dto);
        if (flag){
            return R.success("购买成功！");
        }else {
            return R.error("购买失败");
        }
    }
}

